package com.google.code.mochaccino.framework.jms;
/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.google.code.mochaccino.framework.transaction.TransactionAspect;
import com.google.code.mochaccino.framework.transaction.TransactionListener;
import com.google.code.mochaccino.framework.transaction.TransactionStatus;
import com.google.code.mochaccino.framework.transaction.exception.TransactionManagementException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.jms.BytesMessage;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;

/** JTA Aware Broker Client. */
public final class JmsClientJTA {
	private JmsClient client;

	/** Constructor */
	public JmsClientJTA( ConnectionFactory factory, String name, boolean isQueue, JmsDeliveryMode mode ) throws JMSException {
		this( factory, name, true, isQueue, mode );
	}

	/** Constructor */
	public JmsClientJTA( ConnectionFactory factory, String name, boolean transacted, boolean isQueue, JmsDeliveryMode mode ) {
		if ( !TransactionAspect.isStarted() ) {
			throw new IllegalStateException( "NO Transaction!" );
		}

		this.client = new JmsClient( factory, name, transacted, isQueue, mode );
		this.client.open( JmsSessionMode.AUTO );

		try {
			TransactionAspect.registerListener( new TransactionListener() {
				@Override
				public void beforeCompletion() {
					JmsClientJTA.this.client.close();
				}

				@Override
				public void afterCompletion( TransactionStatus status ) {
				}
			} );
		} catch ( TransactionManagementException e ) {
			throw new RuntimeException( e.getMessage() );
		}
	}

	public BytesMessage createBytes() throws JMSException {
		return client.createBytes();
	}

	public BytesMessage createBytes( byte[] payload ) throws JMSException {
		return client.createBytes( payload );
	}

	public MapMessage createMapMessage() throws JMSException {
		return client.createMapMessage();
	}

	public MapMessage createMapMessage( Map<String,Serializable> payload ) throws JMSException {
		return client.createMapMessage( payload );
	}

	public ObjectMessage createObject() throws JMSException {
		return client.createObject();
	}

	public ObjectMessage createObject( Serializable payload ) throws JMSException {
		return client.createObject( payload );
	}

	public StreamMessage createStream() throws JMSException {
		return client.createStream();
	}

	public StreamMessage createStream( InputStream payload ) throws JMSException, IOException {
		return client.createStream( payload );
	}

	public TextMessage createText() throws JMSException {
		return client.createText();
	}

	public TextMessage createText( String payload ) throws JMSException {
		return client.createText( payload );
	}

	public int post( Message... messages ) throws JMSException {
		return client.post( messages );
	}

	public int post( String destinationName, boolean isQueue, Message... messages ) throws JMSException {
		return client.post( destinationName, isQueue, messages );
	}

	public Message receive() throws JMSException {
		return client.receive();
	}

	public Message receive( TimeUnit unit, long timeout ) throws JMSException {
		return client.receive( unit, timeout );
	}

	public Message receiveNoWait() throws JMSException {
		return client.receiveNoWait();
	}
}
